 /**
 * Description: 附件管理
 * @author:jacky
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-06-06   jacky       1.0.0     Initial
 */
package com.metro.demo.cust.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.demo.cust.mapper.IBizCustAttachmentMapper;
import com.metro.demo.cust.service.inf.IBizCustAttachmentService;
import com.metro.demo.material.beans.vo.BizMaterialManage;
import com.metro.demo.common.beans.constant.PropertiesEnum;
import com.metro.demo.cust.beans.vo.BizCustAttachment;

 /**
 * 附件管理<br>
 *
 * @author:jacky
 * @since:1.0.0
 * @version [1.0.0, 2018-06-06]
 */
@Service("bizcustattachmentService")
public class BizCustAttachmentService extends BaseServiceSupport<BizCustAttachment> implements IBizCustAttachmentService{
    //附件管理Mapper
    @Autowired
    private IBizCustAttachmentMapper bizcustattachmentMapper;

    @Override
    public IMapper getMapper() {
        return bizcustattachmentMapper;
    }

    @Override
    public IResult save(BizCustAttachment entity) {
        try {
            bizcustattachmentMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增附件管理失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(BizCustAttachment entity) {
        try {
            bizcustattachmentMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改附件管理失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            bizcustattachmentMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            bizcustattachmentMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
  //查询图片路径
  	@Override
  	public List<BizCustAttachment> queryById(Long id) {
  		
  		 try {
  			 List<BizCustAttachment> list = bizcustattachmentMapper.queryById(id);
  	            return list;
  	        } catch (Exception e) {
  	            log.error("查询失败", e);
  	            throw new ServiceException(ResultCode.ERROR);
  	        }
  	}
    
  	 /**
  	  * 删除附件
  	  * @param entity
  	  * @return
  	  */
  	 @Override
  	    public IResult deleteAttachment(String id) {
  	        try {
  	        	//根据custid删除数据库数据
  	        	bizcustattachmentMapper.deleteByCustId(Long.valueOf(id));
  	        	//修改manage表为无附件
  	        	
  	          //删除本地图片 删除到code目录
  	        	String deletePath = PropertiesEnum.getPropertyValue(PropertiesEnum.FILE_UPLOAD_ROOT)+"/cust/"+id;  
  	            //删除文件夹
  	            delFolder(deletePath);
  	            return ResultCode.SUCCESS;
  	        } catch (Exception e) {
  	            log.error("删除失败", e);
  	            throw new ServiceException(ResultCode.ERROR);
  	        }
  	    }
  	//删除文件夹  
  	 public static void delFolder(String folderPath) {  
  	      try {  
  	         delAllFile(folderPath); //删除完里面所有内容  
  	         String filePath = folderPath;  
  	         filePath = filePath.toString();  
  	         java.io.File myFilePath = new java.io.File(filePath);  
  	         myFilePath.delete(); //删除空文件夹  
  	      } catch (Exception e) {  
  	        e.printStackTrace();   
  	      }  
  	 } 
  	 
  	 public static boolean delAllFile(String path) {  
  	       boolean flag = false;  
  	       File file = new File(path);  
  	       if (!file.exists()) {  
  	         return flag;  
  	       }  
  	       if (!file.isDirectory()) {  
  	         return flag;  
  	       }  
  	       String[] tempList = file.list();  
  	       File temp = null;  
  	       for (int i = 0; i < tempList.length; i++) {  
  	          if (path.endsWith(File.separator)) {  
  	             temp = new File(path + tempList[i]);  
  	          } else {  
  	              temp = new File(path + File.separator + tempList[i]);  
  	          }  
  	          if (temp.isFile()) {  
  	             temp.delete();  
  	          }  
  	          if (temp.isDirectory()) {  
  	             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件  
  	             delFolder(path + "/" + tempList[i]);//再删除空文件夹  
  	             flag = true;  
  	          }  
  	       }  
  	       return flag;  
  	     }
  	
}